﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CommonTagTime;
using CommonTagTime.Business;
using CommonTagTime.ModelObject;


public partial class Sitemap : SitePage
{
    public string htmlProduct = string.Empty;
    public string htmlVideo = string.Empty;
    public string htmlArea = string.Empty;   
    public string type = "category";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
          
            htmlProduct=LoadCategoryByType((byte)enmCategoryType.Product, true);
            htmlVideo = LoadCategoryByType((byte)enmCategoryType.Video, true);
            htmlArea = LoadCategoryByType((byte)enmCategoryType.Area, true);
        }
    }
    public string LoadCategoryByType(byte typeCate,bool isExpand)
    {
        string link = string.Empty;
        string result = string.Empty;
        //Load all Catgeory in Level 1
        if (typeCate == (byte)enmCategoryType.Product)
        {
            link = "../product";
        }
        else if (typeCate == (byte)enmCategoryType.Video)
        {
            link = "../video";
            type = "video";
        }
        else if (typeCate == (byte)enmCategoryType.Area)
        {
            type = "shop";
            link = "../shop";
        }
        Category objCondition = new Category();
        objCondition.OrderBy = busCategory.SORTORDER;
        objCondition.OrderDirection = "ASC";
        objCondition.CurrentPage = 1;
        objCondition.PageSize = int.MaxValue;
        objCondition.Condition = " ParentId is null and Type=" + typeCate;
        busCategory objBus = new busCategory();
        CategoryCollection lstLevel1 = objBus.List(objCondition);//only return one object
        if (lstLevel1 != null && lstLevel1.Count > 0)
        {
            result = @"<table cellspacing='0' cellpadding='0' border='0'>
                        <tr>
                            <td>
                                  <img height='22' width='16' src='../App_Themes/Admin/images/icons/folderminus2.gif' id='FFLD" + lstLevel1[0].CategoryID + @"'>
                            </td>
                            <td>
                                    <img height='16' width='4' src='../App_Themes/Admin/images/icons/spacer.gif'>
                            </td>                           
                            <td nowrap='' class='category-box'>
                                    <a href='" + link + @"'>" + lstLevel1[0].Name + @"</a>
                            </td>                           
                            </tr>
                       </table>";
            CategoryCollection lstChild = GetChild(lstLevel1[0], typeCate);
            int position = 1;
            for (int i = 0; i < lstChild.Count; i++)
            {
                Category objBreadCrum = lstChild[i];
                if (i == lstChild.Count - 1)
                    position = 3;
                else if (i == 0)
                    position = 1;
                else position = 2;
                result += BuildHTML(objBreadCrum, isExpand, position, lstChild.Count, typeCate);
            }
        }
        return result;
    }
    private string BuildHTML(Category objData, bool isExpand, int position, int lstCount,byte type)
    {
        bool lastNode = false;
        //position: 1:at the begin in list,2: at the middle, 3: at the end
        if (position == 3)
            lastNode = true;
        string tdPadding = string.Empty;
        string result = string.Empty;
        for (int i = 2; i < objData.Level; i++)
        {
            tdPadding += @" <td>
                    <img height='22' width='19' src='../App_Themes/Admin/images/icons/i2.gif'>
                </td>";
        }

        //get list child for this object
        CategoryCollection lstChild = GetChild(objData,type);
        if (lstChild != null && lstChild.Count > 0)
        {
            string folderexpand = "folderplus2.gif";
            string expandImage = string.Empty;
            if (isExpand)
            {
                folderexpand = "folderminus2.gif";
            }
            if (objData.Level > 1 && lastNode)
            {
                expandImage = "  <img height='22' width='19' id='IMG" + objData.CategoryID + @"' src='../App_Themes/Admin/images/icons/lminus2.gif'>";
            }
            else
            {
                if (isExpand)
                {
                    // folderexpand = "folderminus2.gif";
                    expandImage = "  <img height='22' width='19' id='IMG" + objData.CategoryID + @"' src='../App_Themes/Admin/images/icons/tminus2.gif'>";
                }
                else
                {
                    // folderexpand = "folderplus2.gif";
                    expandImage = "  <img height='22' width='19' id='IMG" + objData.CategoryID + @"' src='../App_Themes/Admin/images/icons/tplus2.gif'>";
                }
            }
            // expandImage = "  <img height='22' width='19' id='IMG" + objData.CategoryID + @"' src='../App_Themes/Admin/images/icons/lminus2.gif'>";
            result += @"<table cellspacing='0' cellpadding='0' border='0'>
            <tr>
                " + tdPadding + @"
                <td  onclick=expandit(" + objData.CategoryID + @")>
                    " + expandImage + @"
                </td>
                <td>
                    <img height='22' width='16' id='FLD" + objData.CategoryID + @"' src='../App_Themes/Admin/images/icons/" + folderexpand + @"'>
                </td>                 
                <td nowrap='' class='category-box'>
                    <a href='" + BuildLink(objData, type) + @"'>" + objData.Name + @"</a>
                </td>               
            </tr>
        </table>";
            if (isExpand)
                result += "<span id='SPAN" + objData.CategoryID + "' style='display:block;'>";
            else result += "<span id='SPAN" + objData.CategoryID + "' style='display: none;'>";
            int subposition = 1;
            for (int index = 0; index < lstChild.Count; index++)
            {
                if (index == lstChild.Count - 1)
                    subposition = 3;
                else if (index == 0)
                    subposition = 1;
                else subposition = 2;
                Category objBreadCrum = lstChild[index];
                result += BuildHTML(objBreadCrum, isExpand, subposition, lstChild.Count,type);
            }
            result += "</span>";
        }
        else 
        {
            string expandImage = string.Empty;
            if (lastNode)
            {
                expandImage = "  <img height='22' width='19'  src='../App_Themes/Admin/images/icons/l2.gif'>";
            }
            else expandImage = "  <img height='22' width='19'  src='../App_Themes/Admin/images/icons/t2.gif'>";

            result = @"<table cellspacing='0' cellpadding='0' border='0'>
            <tr>
                " + tdPadding + @"
                <td>
                   " + expandImage + @"
                </td>
                <td>
                    <img height='22' width='16' src='../App_Themes/Admin/images/icons/folderplus2.gif'>
                </td>               
                <td nowrap='' class='category-box'>
                    <a href='" + BuildLink(objData, type) + @"'>" + objData.Name + @"</a>
                </td>             
            </tr>
        </table>";
        }
        return result;
    }
    private CategoryCollection GetChild(Category objData, byte type)
    {
        Category objCondition = new Category();
        objCondition.OrderBy = busCategory.SORTORDER;
        objCondition.OrderDirection = "ASC";
        objCondition.Condition = " ParentId=" + objData.CategoryID;
        objCondition.CurrentPage = 1;
        objCondition.PageSize = int.MaxValue;
        busCategory objBus = new busCategory();
        CategoryCollection lstResult = objBus.List(objCondition);
        if(lstResult!=null && lstResult.Count>0)
            return lstResult;
        else if (type == (byte)enmCategoryType.Area)
        {
            return GetCountryForCate(objData);            
        }
        return null;        
    }
    public CategoryCollection GetCountryForCate(Category objParent)
    {
        CategoryCollection lstResult = new CategoryCollection();
        busCountryAreaCategory objBus = new busCountryAreaCategory();
        CountryAreaCategoryCollection lstAreaCate = objBus.GetByCategoryId(objParent.CategoryID);
        if (lstAreaCate != null && lstAreaCate.Count > 0)
        {
            busCountry objBusCountry = new busCountry();
            busDistributor objBusShop = new busDistributor();
            foreach (CountryAreaCategory objCateArea in lstAreaCate)
            {
                Country objCountry = objBusCountry.GetById(objCateArea.CountryID);
                if (objCountry != null)
                {
                    //count Shop for this Country
                    int count = 0;
                    DistributorCollection lstDis = objBusShop.GetByCountryId(objCountry.CountryID);
                    foreach (Distributor objDis in lstDis)
                    {
                        if (objDis.IsActive)
                            count++;
                    }
                    if (count > 0)
                    {
                        Category objCate = new Category();
                        objCate.CategoryID = objCountry.CountryID;
                        objCate.Name = objCountry.CountryName;
                        objCate.Level = (byte)(objParent.Level + 1);
                        objCate.Type = (byte)enmCategoryType.Area;
                        lstResult.Add(objCate);
                    }
                }
            }
        }
        return lstResult;
    }
    public string BuildLink(Category objCate, byte typeCate)
    {
        string result = string.Empty;
        if (typeCate == (byte)enmCategoryType.Area)
        {
            if (objCate.Type == (byte)enmCategoryType.Area)
            {
                busCountryAreaCategory objBusAreaCate = new busCountryAreaCategory();
                CountryAreaCategoryCollection lstCateArea = objBusAreaCate.GetByCountryId(objCate.CategoryID);
                if (lstCateArea != null && lstCateArea.Count > 0)
                {
                    result = "/" + type + "/" + clsText.ToSEO(objCate.Name) + "/" + lstCateArea[0].AreaCategoryId + "/" + objCate.CategoryID;
                }
            }
            else
                result = "/" + type + "/" + clsText.ToSEO(objCate.Name) + "/" + objCate.CategoryID + "/0";
        }
        else result = "/" + type + "/" + clsText.ToSEO(objCate.Name) + "/" + objCate.CategoryID;
        return result;
    }
}